﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Core.SimpleTemp.Api.Api.YH
{
    /// <summary>
    /// 
    /// </summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]

    public class YHDropDownController : CoreApiController
    {
        private CoreDBContext _coreDBContext;
        private YH_BasisTypeService _BasisTypeService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="coreDBContext"></param>
        /// <param name="BasisTypeService"></param>
        public YHDropDownController(CoreDBContext coreDBContext, YH_BasisTypeService BasisTypeService)
        {
            _coreDBContext = coreDBContext;
            _BasisTypeService = BasisTypeService;
        }



        #region 操作与相关作业活动管理

        /// <summary>
        /// 操作与相关作业活动管理 /作业步骤:
        /// </summary>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("OperationAndWorkJobSteps/{factoryId}")]

        public async Task<IActionResult> OperationAndWorkJobSteps(Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_OperationAndWork.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            var ret = await baseQeury.Select(a => new { Value = a.JobSteps }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        /// <summary>
        /// 操作与相关作业活动管理 /作业任务:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("OperationAndWorkTask/{str?}/{factoryId?}")]

        public async Task<IActionResult> OperationAndWorkTask(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_OperationAndWork.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.JobSteps.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.Task }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        #endregion


        #region 检查维修作业活动列表
        /// <summary>
        /// 检查维修作业活动列表 /作业模块::
        /// </summary>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("CheckRepairJobModule/{factoryId?}")]

        public async Task<IActionResult> InspeMainOperaDeviceName(Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_CheckRepair.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);

            var ret = await baseQeury.Select(a => new { Value = a.JobModule }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 检查维修作业活动列表 /维修项目:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("CheckRepairRepairItems/{str?}/{factoryId?}")]

        public async Task<IActionResult> CheckRepairRepairItems(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_CheckRepair.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.JobModule.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.RepairItems }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        /// <summary>
        /// 检查维修作业活动列表 /维修活动:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("CheckRepairJobActivities/{str?}/{factoryId?}")]

        public async Task<IActionResult> CheckRepairJobActivities(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_CheckRepair.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.RepairItems.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.JobActivities }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }




        #endregion

        #region 设备设施
        /// <summary>
        /// 设备设施 /设备
        /// </summary>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("EquipmentEquipment/{factoryId?}")]

        public async Task<IActionResult> EquipmentEquipment(Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_Equipment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            var ret = await baseQeury.Select(a => new { Value = a.Equipment }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 设备设施 /功能部分
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("EquipmentFunctionalPart/{str?}/{factoryId?}")]

        public async Task<IActionResult> EquipmentFunctionalPart(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_Equipment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Equipment.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.FunctionalPart }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        /// <summary>
        /// 设备设施 /子功能部分
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("EquipmentSubfunctionalPart/{str?}/{factoryId?}")]

        public async Task<IActionResult> EquipmentSubfunctionalPart(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_Equipment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.FunctionalPart.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.SubfunctionalPart }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        #endregion



        #region 工艺介质


        /// <summary>
        /// 工艺介质 /要素特性:
        /// </summary>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("ProcessFactor/{factoryId?}")]

        public async Task<IActionResult> ProcessFactor(Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_Process.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            var ret = await baseQeury.Select(a => new { Value = a.Factor }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }



        ///// <summary>
        ///// 工艺介质 引导词
        ///// </summary>
        ///// <param name="str"></param>
        ///// <param name="factoryId"></param>
        ///// <returns></returns>
        //[HttpGet("ProcessIntroducer/{str?}/{factoryId?}")]

        //public async Task<IActionResult> ProcessIntroducer(string str, Guid? factoryId)
        //{
        //    var baseQeury = _coreDBContext.YH_Process.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
        //    if (str != null)
        //        baseQeury = baseQeury.Where(a => a.Factor.Equals(str));
        //    var ret = await baseQeury.Select(a => new { Value = a.Introducer }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
        //    return this.JsonSuccess(ret);
        //}


        /// <summary>
        /// 工艺介质 /偏差
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("ProcessDeviation/{str?}/{factoryId?}")]

        public async Task<IActionResult> ProcessDeviation(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_Process.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.Factor.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.Deviation }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }

        #endregion


        #region 场所环境



        /// <summary>
        ///  场所环境 /场所/环境:
        /// </summary>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("EnvironmentEnvironment/{factoryId?}")]

        public async Task<IActionResult> EnvironmentEnvironment(Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_Environment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            var ret = await baseQeury.Select(a => new { Value = a.PlaceEnvironment }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        /// <summary>
        /// 场所环境 /能量源:
        /// </summary>
        /// <param name="str"></param>
        /// <param name="factoryId"></param>
        /// <returns></returns>
        [HttpGet("EnvironmentEnergySource/{str?}/{factoryId?}")]

        public async Task<IActionResult> EnvironmentEnergySource(string str, Guid? factoryId)
        {
            var baseQeury = _coreDBContext.YH_Environment.Where(a => a.IsDeleted == false && a.CreateFactory == factoryId);
            if (str != null)
                baseQeury = baseQeury.Where(a => a.PlaceEnvironment.Equals(str));
            var ret = await baseQeury.Select(a => new { Value = a.EnergySource }).Distinct().OrderBy(a => a.Value).ToArrayAsync();
            return this.JsonSuccess(ret);
        }


        #endregion



        
    }
}